package kr.redmist.myfavtwit;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import twitter4j.ResponseList;
import twitter4j.Twitter;
import twitter4j.TwitterException;


public class MainActivity extends ActionBarActivity {

    private TweetAdapter tweetAdapter;
    private Twitter twitter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gridview);

        /*if (!TwitterUtils.hasAccessToken(this)) {
            Intent intent = new Intent(this, OAuthActivity.class);
            startActivity(intent);
            finish();
        } else*/ {
            tweetAdapter = new TweetAdapter(this);

            GridView gridview = (GridView)findViewById(R.id.gridview);
            gridview.setAdapter(tweetAdapter);
            gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(MainActivity.this, position + "", Toast.LENGTH_SHORT).show();
                }
            });
            //setListAdapter(tweetAdapter);
            //twitter = TwitterUtils.getTwitterInstance(this);
            //refreshTimeline();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void refreshTimeline() {
        AsyncTask<Void, Void, List<String>> task = new AsyncTask<Void, Void, List<String>>() {
            @Override
            protected List<String> doInBackground(Void... params) {
                try {
                    ResponseList<twitter4j.Status> favorites = twitter.getFavorites();
                    List<String> result = new ArrayList<String>();
                    for (twitter4j.Status favorite : favorites) {
                        result.add(favorite.getText());
                    }
                    return result;
                } catch (TwitterException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(List<String> result) {
                if (result != null) {
                    //tweetAdapter.clear();
                    for (String text : result) {
                    //    tweetAdapter.add(text);
                    }
                    //getListView().setSelection(0);
                } else {

                }
            }
        };
        task.execute();
    }

    /*private class TweetAdapter extends ArrayAdapter<String> {
        public TweetAdapter(Context context) {
            super(context, android.R.layout.simple_list_item_multiple_choice);
        }
    }*/

}
